package com.shuyu.common.utils.aliyun;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
  
  public class AlipayTradeAppPay {
      private static String PRIVATE_KEY = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC6XWClbnUlV7mxLES8u44nUPTq0djnBm6UdLnN1vCF5KX4Yh6r4vYjuPm8fE45WzppP0wMxB7TPTMuzl6XTEnX6T2ce8SR10D0Pkh+nJ0sK90Lste1rRfPJ9aGfLsskXbmsNPmcCyG+2WVmkIQJGf4lqcX1PeHO5Bz9pCs3VFXtIJGHLxrMvHv3THtkz58R6MT37n+r4gbDyew4El0+Qe1bgRi/o+DLwEpVDfAuSZLSWqNl3F9uGW90fcfwjRrA2PV78hFAMB34E5//2srVQ2sW+j4DghNgWKNPJtMq6c+RSvQ9LbcVRE1ApPQ/juIoH8ikuYdU/WEABHGWnCfzvMhAgMBAAECggEAA74ERs4lBA7y55/f8XpCrH9vq5dzJ5B1f/2oDX4XbljdDFPMEd+I2cbJHQPR/kzaq2pd+JOnms5LzNDQDXG2ueMxHYdOty7/Wug1RHWYol5vHeEFe9xx8fL219SBXa++F3vNhX8TJdK+g3aJhRF2FQPRbyUTo1+K0wlMdmwrSfuJKObGSOfqqWfpPUBAREzZe/pWqqQuOs1SlHEM1Z5iRt/vXmHWJ+9w0DAJAOVDJVWLl9ikaCtrEtrOh5fnvRQI5yNw84qZ8IxOEhSlMN2+xxmPHScD465MH4lyo3qlzg+ETJrn52UTxRyvuRRz6ixjaHF0vsfTeFMHB/u2JEpDAQKBgQDnGOfrFLIOltMA+QThl8OcMhOodIfqdwjFNjdsRv6O6r2WcPLYAiIGA1L2boEjALe49fwUolItkx14wck1JQK7Z7666qlM6GqDNEGhgjw4mdb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QXm6VCSTiaL8vaId1bytyzFuPAoKt4MkTTDwCabKjcFifXdf8z91/Q8iaTANQqcf96aSEi2BHiKJhzzMCywc2YCP2UeqaUMO2TByFK7lanWyQIDAQAB";
      private static String APPID = "2021004147680429";

      public static void main(String[] args) throws AlipayApiException {
          pay("2220240709144332273770", "测试测试", "10.00",null);
      }

      public static AlipayTradeAppPayResponse pay(String outTradeNo, String subject, String totalAmount,String notifyUrl) throws AlipayApiException {
          AlipayConfig alipayConfig = new AlipayConfig();
          alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
          alipayConfig.setAppId(APPID);
          alipayConfig.setPrivateKey(PRIVATE_KEY);
          alipayConfig.setFormat("json");
          alipayConfig.setAlipayPublicKey(ALIPAY_PUBLIC_KEY);
          alipayConfig.setCharset("UTF-8");
          alipayConfig.setSignType("RSA2");
          AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
          AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
          AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
          model.setOutTradeNo(outTradeNo);
          model.setTotalAmount(totalAmount);
          model.setSubject(subject);
          request.setBizModel(model);
          request.setNotifyUrl(notifyUrl);
          AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
          return response;
//          String orderStr = response.getBody();
//          System.out.println(orderStr);
//          if (response.isSuccess()) {
//              System.out.println("调用成功");
//          } else {
//              System.out.println("调用失败");
//              // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
//              // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
//              // System.out.println(diagnosisUrl);
//          }
      }


  }